package csu.web.mypetstore.web.servlet.Filter;

import csu.web.mypetstore.domain.*;
import csu.web.mypetstore.service.LogService;

import javax.servlet.*;
import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.logging.*;

public class LoggingFilter implements Filter {

    private LogService logService = new LogService();
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        StringBuffer reqURL = httpRequest.getRequestURL();
        String url = reqURL.toString();
        int index = url.lastIndexOf("/"); // 获取最后一个斜杠的位置
        String req = url.substring(index + 1); // 截取最后一个斜杠之后的字符串

        chain.doFilter(request,response);

        HttpSession session = ((HttpServletRequest) request).getSession();
        Account loginAccount = (Account) session.getAttribute("loginAccount");

        if(loginAccount!=null)
        {
            switch (req){
                case "categoryForm":{
                    Category category = (Category) session.getAttribute("category");
                    logService.addLog(loginAccount.getUsername()+" browsed "+category.getCategoryId());
                    break;
                }
                case "productForm":{
                    Product product = (Product) session.getAttribute("product");
                    logService.addLog(loginAccount.getUsername()+" browsed "+product.getName());
                    break;
                }
                case "itemForm":{
                    Item item = (Item) session.getAttribute("item");
                    logService.addLog(loginAccount.getUsername()+" browsed "+item.getItemId());
                    break;
                }
                case "addItemToCart":{
                    Item item = (Item)session.getAttribute("item");
                    logService.addLog(loginAccount.getUsername()+" added the item "+item.getItemId()+" to the cart");
                    break;
                }
                case "removeCartItem":{
                    String itemID = (String) session.getAttribute("workingItemID");
                    logService.addLog(loginAccount.getUsername()+" removed the item "+itemID+" from the cart");
                    break;
                }
                case "signOn", "signOnByPhone":{
                    logService.addLog(loginAccount.getUsername()+" has logged in");
                    break;
                }
                case "newOrderServlet":{
                    Order order = (Order) session.getAttribute("order");
                    logService.addLog(loginAccount.getUsername()+" has confirmed an order,orderID: " + order.getOrderID());
                    break;
                }
                default:{
                    break;
                }
            }
        }

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void destroy() {}
}